package com.grape.api.v1;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.grape.domain.AccountBook;
import com.grape.domain.CategorySetting;
import com.grape.service.ICategorySettingService;
import com.grape.annotation.OperateMethodName;
import com.grape.controller.BaseController;
import com.grape.result.R;
import com.grape.vo.BaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.util.Strings;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.grape.annotation.RepeatSubmit;

import javax.validation.constraints.NotBlank;
import java.util.List;


/**
 * 分类表 控制器
 * @author      putao
 * @email       2628279194@qq.com
 */
@RestController
@RequestMapping("categorySettings")
@Api(tags = "分类接口")
@AllArgsConstructor
public class CategorySettingController extends BaseController<CategorySetting> {

    private final ICategorySettingService categorySettingService;


    @GetMapping
    @ApiOperation(value = "分类分页")
    @OperateMethodName(value = "分类表列表分页", url = "account:categorySettings:listPage")
    public R<?> listPage(BaseVo baseVo) {
        PageInfo<CategorySetting> pageInfo = categorySettingService.pageList(baseVo);
        List<CategorySetting> categorySettingList = pageInfo.getList();
        return CollectionUtil.isNotEmpty(categorySettingList) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @GetMapping("listPageCustom")
    @ApiOperation(value = "分类自定义分页")
    @OperateMethodName(value = "分类表列表", url = "account:categorySettings:listPageCustom")
    public R<?> pageListCustom(BaseVo baseVo) {
        Page<CategorySetting> pages = PageHelper.startPage(baseVo.getPageNum(), baseVo.getPageSize());
        categorySettingService.pageListCustom(baseVo);
        PageInfo<CategorySetting> pageInfo = pages.toPageInfo();
        return CollectionUtil.isNotEmpty(pageInfo.getList()) ? selectSuccess(pageInfo, baseVo) : selectNotFound(pageInfo, baseVo);
    }


    @RepeatSubmit
    @PutMapping
    @ApiOperation(value = "分类修改")
    @OperateMethodName(value = "分类表修改", url = "account:categorySettings:update")
    public R<?> update(@Validated @RequestBody CategorySetting categorySetting) {
        if (!Strings.isNotBlank(categorySetting.getId())) {
            return updateNotFound(categorySetting, null);
        }
        categorySettingService.updateData(categorySetting);
        return updateSuccess(categorySetting, null);
    }


    @RepeatSubmit
    @PostMapping
    @ApiOperation(value = "添加分类")
    @OperateMethodName(value = "添加分类表", url = "account:categorySettings:insert")
    public R<?> insert(@Validated @RequestBody CategorySetting categorySetting) {
        return insertSuccess(categorySettingService.insertData(categorySetting), null);
    }


    @DeleteMapping
    @ApiOperation(value = "删除分类，可批量多个id逗号拼接")
    @OperateMethodName(value = "分类表删除,批量", url = "account:categorySettings:delete")
    public R<?> delete(@NotBlank(message = "删除时ids不能为空") @RequestParam String ids) {
        int number = categorySettingService.deleteData(ids);
        if (number < 1) {
            return deleteNotFound(ids, number);
        }
        return deleteSuccess(ids, null);
    }


    @GetMapping("detail")
    @ApiOperation(value = "分类详情")
    @OperateMethodName(value = "分类表详情", url = "account:categorySettings:detail")
    public R<?> detail(@NotBlank(message = "详情时id不能为空") String id) {
        CategorySetting categorySetting = categorySettingService.detail(id);
        if (categorySetting != null) {
            return selectSuccess(categorySetting, id);
        }
        return selectNotFound(id, null);
    }


    @GetMapping("listAll")
    @ApiOperation(value = "账本全部列表")
    @OperateMethodName(value = "账本全部列表", url = "account:categorySettings:listAll")
    public R<?> listAll() {
        List<CategorySetting> list = categorySettingService.listAll();
        return CollectionUtil.isNotEmpty(list) ? selectSuccess(list, null) : selectNotFound(list, null);
    }
}

